﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

namespace FieldDevice.UI
{
    internal class UIManager
    {
        Form RootForm;
        DeviceManager DeviceManager; 
        public UIManager(Form rootform)
        {
            this.DeviceManager = DeviceManager.GlobalDeviceManager;
            RootForm = rootform;
        }

        internal void monitorDevice(Device device)
        {
            FieldLib.Comm.CommWatchForm form = new FieldLib.Comm.CommWatchForm(device);
            form.Show(this.RootForm);
            ((FieldLib.Comm.IChannel)device).AddWatch(form);
            form.FormClosing += new FormClosingEventHandler(form_FormClosing);
        }

        void form_FormClosing(object sender, FormClosingEventArgs e)
        {
            FieldLib.Comm.CommWatchForm form = (FieldLib.Comm.CommWatchForm)sender;
            form.SerialPort.RemoveWatch(form);
        }
        public CurrentBarForm CurrentBar;
        SortedList<uint, DeviceFormBase> DeviceForms = new SortedList<uint, DeviceFormBase>();
        internal DeviceFormBase OpenDevice(Device device)
        {
            UI.DeviceFormBase form = null;
            if (!this.DeviceForms.TryGetValue(device.ModbusAddress, out form))
            {
                switch (device.Category)
                {
                    case Device.DeviceCategory.DCDevice:
                        form = new DCDeviceForm(device);
                        break;
                    case Device.DeviceCategory.Temperature:
                        form = new TemperatureForm(device);
                        break;
                    case Device.DeviceCategory.HighVoltageDevice:
                        form = new DeviceForm(device);
                        break;
                    case Device.DeviceCategory.REF615:
                        form = new REF615Forms(device);
                        break;
                    default:
                        form = new FieldDevice.UI.DeviceForm(device);
                        break;
                }
                form.FormClosed += new FormClosedEventHandler(form_FormClosed);
                this.DeviceForms.Add(device.NCPAddress, form);
                form.Show(this.RootForm);
            }
            else
                form.BringToFront();
            return form;
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            lock (this.DeviceForms)
            {
                DeviceFormBase deviceform = (DeviceFormBase)sender;
                this.DeviceForms.Remove(deviceform.Device1.NCPAddress);
            }
        }

        public void OnTimer()
        {
            lock (this.DeviceForms)
            {
                //foreach (Device device in this..Devices.Values)
                //{
                //    if (device.Dirty)
                //        device.FireReadChangeEvent();
                //}
                foreach (DeviceFormBase deviceform in this.DeviceForms.Values)
                {
                    if (deviceform.Device1.Dirty)
                    {
                        deviceform.UpdateReads();
                    }
                }
                if (CurrentBar != null) 
                    CurrentBar.UpdateReads();
            }
                
        }
    }
}
